import { environment } from 'environments/environment';
import { store } from 'spd-store';
import { POST, Auth } from 'spd-request';
import { useLocalStore } from 'mobx-react';
export function useLogin() {
    const localStore = useLocalStore(() => store);
    return async (username: string, password: string): Promise<any> => {
        let body = {
            captchaModel: {
                captchaId: '',
                userEnteredCaptchaCode: ''
            },
            password: password,
            rememberClient: false,
            returnUrl: null,
            singleSignIn: false,
            twoFactorRememberClientToken: null,
            twoFactorVerificationCode: null,
            type: 1,
            userNameOrEmailAddress: username
        }
        return await POST(environment.serveHost + '/api/TokenAuth/Authenticate', body).then(resp => {
            if(resp.success === true) {
                localStore.setLoginUser(resp.result);
                Auth.updateAccessToken(resp.result.accessToken);
                return {error: null, result: resp.result};
            } else {
                return {error: true, result: resp.result};
            }
        });
    }
}